
package com.putao.album.main;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.child.ActivityFamily;
import com.putao.album.child.bean.FamilyItem;
import com.putao.album.dialog.PtTagManagerDialog;
import com.putao.album.main.bean.AlbumListItem;
import com.putao.album.main.view.HomeFamilyLinearLayout;
import com.putao.album.main.view.MultiPhotoListItemView;
import com.putao.album.tag.ActivityTagAlbumPhotoList;
import com.putao.album.tinyalbum.ActivityTinyAlbum;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.StringHelper;
import com.putao.album.util.UmengTouchEvent;
import com.putao.album.widget.pttag.PtTag;
import com.putao.album.widget.pttag.PtTagListView;
import com.putao.album.widget.pttag.PtTagView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class HomeFragmentListAdapter extends BaseAdapter {
    private ArrayList<AlbumListItem> mList = new ArrayList<AlbumListItem>();
    private Context mContext;
    private Activity mActivity;
    private View.OnClickListener itemClickListener;
    private ArrayList<FamilyItem> familyItems;

    /**
     * 设置关系头像
     *
     * @param familyItems
     */
    public void setAvatar(ArrayList<FamilyItem> familyItems) {
        this.familyItems = familyItems;
    }

    public HomeFragmentListAdapter(Activity activity) {
        this.mActivity = activity;
        this.mContext = activity;
    }

    public int getDataCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public int getCount() {
        if (mList != null) {
            if (mList.size() == 0) {
                return 1;
            } else {
                return mList.size();
            }
        }
        return 0;
    }

    @Override
    public AlbumListItem getItem(int position) {
        //        Loger.i("mList.size():" + mList.size());
        if (mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (mList.size() == 1) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_home_page_baby_photo_empty_item, null);
            LinearLayout baby_icon_nophoto_ll = (LinearLayout) convertView.findViewById(R.id.baby_icon_nophoto_ll);
            Button empty_invite_btn = (Button) convertView.findViewById(R.id.empty_invite_btn);
            empty_invite_btn.setText(CommonUtils.getRelationShip().equals("妈妈") ? "邀请爸爸" : "邀请妈妈");
            baby_icon_nophoto_ll.setOnClickListener(itemClickListener);
            empty_invite_btn.setOnClickListener(itemClickListener);
            return convertView;
        }
        if (position == 0) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_album_home_family, null);
            HomeFamilyLinearLayout relation_content = (HomeFamilyLinearLayout) convertView.findViewById(R.id.relation_content);
            relation_content.setOnAvatarOnClickListener(new HomeFamilyLinearLayout.OnAvatarOnClickListener() {
                @Override
                public void onClick() {
                    ActivityHelper.startActivity(mActivity, ActivityFamily.class);
                }
            });
            TextView btn_invite_family = (TextView) convertView.findViewById(R.id.btn_invite_family);
            if (familyItems != null && familyItems.size() > 0) {
                relation_content.setFamilyItems(familyItems);
            }
            btn_invite_family.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityHelper.startActivity(mActivity, ActivityInviteChildRelationSelect.class);
                }
            });
            return convertView;
        } else {
            ViewHolder holder = new ViewHolder();
            if (convertView == null || convertView.getTag() == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_home_fragment_list_item, null);
                holder.content_ll = (LinearLayout) convertView.findViewById(R.id.content_ll);
                holder.story_view = (LinearLayout) convertView.findViewById(R.id.story_view);
                holder.home_page_time_name_tv = (TextView) convertView.findViewById(R.id.home_page_time_name_tv);
                holder.home_page_time_tv = (TextView) convertView.findViewById(R.id.home_page_time_tv);
                holder.home_page_owner_tv = (TextView) convertView.findViewById(R.id.home_page_owner_tv);
                holder.home_page_photo_number = (TextView) convertView.findViewById(R.id.home_page_photo_number);
                holder.story_tv = (TextView) convertView.findViewById(R.id.story_tv);

                holder.home_page_tag_btn = (Button) convertView.findViewById(R.id.home_page_tag_btn);
                holder.home_page_exist_tag_view = (PtTagListView) convertView.findViewById(R.id.home_page_exist_tag_view);
                holder.home_page_exist_tag_rl = (RelativeLayout) convertView.findViewById(R.id.home_page_exist_tag_rl);
                holder.multi_photo_list_item_view = (MultiPhotoListItemView) convertView.findViewById(R.id.multi_photo_list_item_view);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final AlbumListItem item = getItem(position);
            holder.multi_photo_list_item_view.setAlbumPicItem(item.albunPicItem);
            holder.home_page_time_name_tv.setText(item.timeName);
            holder.home_page_time_tv.setText(item.time);
            holder.home_page_owner_tv.setText("by " + item.owner);
            holder.home_page_photo_number.setText(item.photo_number);
//            if (StringHelper.isEmpty(item.story)) {
//                holder.story_view.setVisibility(View.INVISIBLE);
//            } else {
//                holder.story_view.setVisibility(View.VISIBLE);
//                holder.story_tv.setText(item.story);
//            }
            holder.home_page_tag_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MobclickAgent.onEvent(mContext, UmengTouchEvent.HOME_ADDLABEL);
                    PtTagManagerDialog dialog = new PtTagManagerDialog.Builder(mContext).setNegativeButton(new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).Create(item.album_id);
                    dialog.show();
                }
            });
            holder.home_page_exist_tag_view = (PtTagListView) convertView.findViewById(R.id.home_page_exist_tag_view);
            holder.home_page_exist_tag_view.setTagViewTextColorRes(R.color.text_color_dark_888888);
            holder.home_page_exist_tag_view.setTagViewBackgroundRes(R.drawable.tag_home_bg);
            holder.home_page_exist_tag_view.setTagViewTextSize(12);

            if (item.label != null && item.label.size() > 0) {
                List<PtTag> mTags = new ArrayList<PtTag>();
                for (int i = 0; i < item.label.size(); i++) {
                    PtTag tag = new PtTag();
                    tag.setId(i);
                    tag.setChecked(true);
                    tag.setTitle(item.label.get(i));
                    mTags.add(tag);
                }
                holder.home_page_exist_tag_rl.setVisibility(View.VISIBLE);
                holder.home_page_tag_btn.setVisibility(View.GONE);
                holder.home_page_exist_tag_view.setTags(mTags);
                holder.home_page_exist_tag_view.setOnTagClickListener(new PtTagListView.OnTagClickListener() {
                    @Override
                    public void onTagClick(PtTagView tagView, PtTag tag) {
                        Bundle bundle = new Bundle();
                        //TODO:传入标签的名称
                        bundle.putString("tag_name", tag.getTitle());
                        bundle.putString("tag_id", String.valueOf(tag.getId()));
                        ActivityHelper.startActivity(mActivity, ActivityTagAlbumPhotoList.class, bundle);
                    }
                });
            } else {
                holder.home_page_exist_tag_rl.setVisibility(View.GONE);
                holder.home_page_tag_btn.setVisibility(View.VISIBLE);
            }
            holder.content_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("albumId", item.album_id);
                    ActivityHelper.startActivity(mActivity, ActivityTinyAlbum.class, bundle);
                }
            });
            return convertView;
        }
    }

    @Override
    public int getItemViewType(int position) {
        AlbumListItem item = getItem(position);
        if (item == null || item.albunPicItem == null) {
            return -1;
        }
        int size = item.albunPicItem.size();
        if (size == 1) {
            return 1;
        } else if (size == 2) {
            return 2;
        } else if (size >= 3 && size < 6) {
            return 3;
        } else {
            return 6;
        }
    }

    public void insertData(AlbumListItem item) {
        if (mList != null) {
            mList.add(1, item);
        }
    }

    public void addData(AlbumListItem item) {
        if (mList != null) {
            mList.add(item);
        }
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
    }

    @Override
    public void notifyDataSetChanged() {
        if (mList != null) {
            if (mList.size() == 0) {
                AlbumListItem item = new AlbumListItem();
                item.album_id = "-1";
                mList.add(0, item);
            } else {
                if (StringHelper.isEmpty(mList.get(0).album_id) || Integer.valueOf(mList.get(0).album_id) > 0) {
                    AlbumListItem item = new AlbumListItem();
                    item.album_id = "-1";
                    mList.add(0, item);
                }
            }
        }
        super.notifyDataSetChanged();
    }

    class ViewHolder {
        public LinearLayout content_ll;
        public LinearLayout story_view;
        public TextView home_page_time_name_tv;
        public TextView home_page_time_tv;
        public TextView home_page_owner_tv;
        public Button home_page_tag_btn;
        public PtTagListView home_page_exist_tag_view;
        public RelativeLayout home_page_exist_tag_rl;
        public TextView home_page_photo_number;
        public TextView story_tv;
        public MultiPhotoListItemView multi_photo_list_item_view;
    }

    public void setItemClickListener(View.OnClickListener clickListener) {
        this.itemClickListener = clickListener;
    }
}
